/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Arek
 */
public class DBConnect {
    private static ResultSet rs; //przechowywane wyniki zapytania do bazy danych
    private Connection sql;
    private PreparedStatement stmt;
  
protected DBConnect()
    {
       try  
        {  
            sql = DriverManager.getConnection("jdbc:mysql://localhost:3306", "adyrka", "mikrus"); //ustanowienie połączenia  
            Statement stat = sql.createStatement(); //utworzenie obiektu statement           
            stat.execute("USE projekt_bazy"); //wybór bazy  
            rs = stat.executeQuery("SELECT * FROM zdjecia"); //pobranie danych z bazy  
            rs.first(); // ustawienie kursora na 1 rekord
            System.out.println("uruchomiony konstruktor"); // wypisanie na standardowe wyjscie inforamcji o tworzeniu obiektu
        }  
        catch(Exception e)  
        {  
            e.printStackTrace();  
        }
   }
   public ResultSet getResults()
    {
        return rs;
    }    
    public void endConnection() throws SQLException
    {
        rs.close(); //zamknięcie strumieni  
        sql.close(); 
    }
    public void saveImage() throws FileNotFoundException, IOException, SQLException
    {
    try {
            
            String sql_query = "SELECT name, image FROM zdjecia ";
            stmt = sql.prepareStatement(sql_query);
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString(1);
                System.out.println("Name        = " + name);


                File image = new File("D:\\java.jpg");
                FileOutputStream fos = new FileOutputStream(image);

                byte[] buffer = new byte[256];

                //
                // Get the binary stream of our BLOB data
                //
                InputStream is = resultSet.getBinaryStream(2);
                while (is.read(buffer) > 0) {
                    fos.write(buffer);
                }
            
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } 
    }
    
    public void uploadImage() throws SQLException, FileNotFoundException
    {
    stmt = sql.prepareStatement("insert into zdjecia(name, image) "+ "values(?,?)");
    FileInputStream fis;
    File image = new File("D:/testjava.jpg");
    
    stmt.setString(1,"test_upload");
    fis = new FileInputStream(image);
    stmt.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
    int s =  stmt.executeUpdate();
    
    if(s>0) {
        System.out.println("Uploaded successfully !");
        }
    else {
        System.out.println("unsucessfull to upload image.");
        }
    
    }
}
